package user;

import static org.junit.Assert.*;

import java.util.*;

import msg.MessageManager;

import org.junit.*;

import registration.RegistrationManager;

import common.*;
import common.dal.Transaction;
import common.exceptions.ExistingEntityException;
import common.interfaces.IPersistent;

@SuppressWarnings({"static-method", "javadoc"})
public class DBPasswordManagerTest extends DBTest {
	private static DBPasswordManager	$;
	
	@Override
	protected IPersistent setPersistent(Transaction t) {
		$ = new DBPasswordManager(t);
		return $;
	}
	
	@Override
	protected IPersistent getPersistent() {
		return $;
	}
	
	@BeforeClass
	public static void setupBeforeClass() throws Exception {
		DALTests.setupBeforeClass();
		t = new Transaction();
		RegistrationManager.getNewInstance(t).clear();
		MessageManager.getNewInstance(t).clear();
		$ = new DBPasswordManager(t);
		t.commit();
	}
	
	@Test
	public void testIsPasswordOf() throws Exception {
		assertFalse($.isPasswordOf("gal", "password"));
		$.registerUser("gal", "Gal Lalouche", "password");
		assertTrue($.isPasswordOf("gal", "password"));
		assertFalse($.isPasswordOf("gal", "password!"));
		assertFalse($.isPasswordOf("gal", "!password"));
		assertFalse($.isPasswordOf("gal", "areyouthisisn'tpassword?"));
		assertFalse($.isPasswordOf("gal", "Password"));
	}
	
	@Test
	public void testIsExistingUser() throws Exception {
		assertFalse($.exists("gal"));
		$.registerUser("gal", "Gal Lalouche", "password");
		assertTrue($.exists("gal"));
	}
	
	@Test(expected = ExistingEntityException.class)
	public void testRegisterUserExisting() throws Exception {
		$.registerUser("gal", "Gal Lalouche", "password");
		$.registerUser("gal", "Gal Lalouche", "password");
	}
	
	@Test
	public void testRegisterUser() throws Exception {
		// $.registerUser("gal", "Gal Lalouche", "password");
		// assertTrue($.exists("gal"));
	}
	
	// @Test
	// public void testRegisterAdmin() throws Exception {
	// $.registerAdmin("gal", "Gal Lalouche", "password");
	// assertTrue($.exists("gal"));
	// }
	
	@Test
	public void testUnregisterUser() throws Exception {
		assertFalse($.unregisterUser("gal"));
		$.registerUser("gal", "Gal Lalouche", "password");
		assertTrue($.unregisterUser("gal"));
		$.registerUser("gal", "Gal Lalouche", "password");
	}
	
	// @Test
	// public void testCheckAdminPassword() {
	// assertFalse($.checkAdminPassword("open sesseimi"));
	// assertFalse($.checkAdminPassword("abra cadabra"));
	// assertTrue($.checkAdminPassword(ADMIN_PASS));
	// }
	
	@Test
	public void testGetUser() throws Exception {
		$.registerUser("gal", "Gal Lalouche", "password");
		assertEquals(new User("gal", ""), $.get("gal"));
	}
	
	// @Test
	// public void testGetAdmin() throws Exception {
	// $.registerAdmin("admin", "Gal Lalouche", "password");
	// $.registerUser("gal", "Gal Lalouche", "password");
	// assertEquals(new Administrator("admin", ""), $.get("admin"));
	// assertFalse(new Administrator("admin", "").equals($.get("gal")));
	// assertFalse(new User("admin", "").equals($.get("admin")));
	// }
	
	@Test
	public void testGetAll() throws Exception {
		
		// $.registerAdmin("gal", "Gal Lalouche", "password");
		$.registerUser("gil", "Gil Frucher", "password");
		final Set<User> results = new HashSet<>($.getAll());
		final Set<User> expected = new HashSet<User>() {
			{
				// add(new Administrator("gal", ""));
				add(new User("gil", ""));
			}
		};
		
		assertEquals(results, expected);
	}
}
